/*
 * @Author: zqx me_zqx@163.com
 * @Date: 2025-05-20 16:13:58
 * @LastEditors: zqx me_zqx@163.com
 * @LastEditTime: 2025-09-08 18:44:11
 * @FilePath: /XMate-h5/src/composables/useScanner.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import QrScanner from "qr-scanner";

export function useScanner(onResult: (result: string) => void) {

  let scanner;
  const startScanning = async (elementId: string) => {
    const videoElem = document.querySelector(`#${elementId}`) as HTMLElement;
    if(!videoElem) {
      console.log("videoElem not found: ", elementId);
      return;
    }
    console.log("videoElem: ", videoElem,elementId);
    scanner = new QrScanner(
      videoElem,
      result => onResult(result),
    );

    scanner.start();
  }

  const stopScanning = () =>  scanner.stop();


  return {
    startScanning,
    stopScanning
  }
} 